package tt.com.warehousemanager.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tt.com.common.enums.AuditEnum;
import tt.com.common.model.YxCommonPurchase;
import tt.com.common.util.AuditUtil;
import tt.com.common.util.PageUtil;
import tt.com.warehousemanager.mapper.PurchaseMapper;
import tt.com.warehousemanager.mapper.PurchaseServeMapper;
import tt.com.warehousemanager.service.PurchaseService;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author Czycka
 * @date 2022/8/19 15:53
 * @description 采购信息实现类
 */
@Service
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, YxCommonPurchase> implements PurchaseService {


    @Override
    public Integer insertPurchase(YxCommonPurchase purchase, String createUserId) {
        // 设置创建时间
        purchase.setCreateTime(new Date());
        // 设置创建操作人id
        purchase.setCreateUserId(createUserId);
        // 设置采购状态
        purchase.setStatus(AuditEnum.PENDING_REVIEW);
        return baseMapper.insert(purchase);

    }

    @Override
    public Integer deletePurchase(Long id, String deleteUserId) {
        // 根据 id 获取数据库内的采购信息
        YxCommonPurchase purchase = baseMapper.selectById(id);
        if (purchase == null) {
            // 若采购信息不存在则返回 -1
            return -1;
        }
        if (purchase.getDataStatus() == 1) {
            // 若采购信息已被删除则返回 -2
            return -2;
        } else {
            // 设置删除时间
            purchase.setDeleteTime(new Date());
            // 设置删除操作人id
            purchase.setDeleteUserId(deleteUserId);
            baseMapper.updateById(purchase);
            return baseMapper.deleteById(purchase.getId());
        }
    }

    @Override
    public Integer updatePurchase(YxCommonPurchase purchase, String updateUserId) {
        // 设置修改时间
        purchase.setLastUpdateTime(new Date());
        // 修改删除状态
        purchase.setLastUpdateUserId(updateUserId);
        return baseMapper.updateById(purchase);
    }

    @Override
    public PageUtil showPurchaseByPage(PageUtil pageUtil) {
        Page<YxCommonPurchase> page = new Page<>(pageUtil.getPageIndex(), pageUtil.getPageSize());
        IPage<YxCommonPurchase> PurchasePage = baseMapper.selectPage(page, null);
        pageUtil.setTotalSize((int) PurchasePage.getTotal());
        pageUtil.setData(PurchasePage);
        return pageUtil;
    }

    @Override
    public Integer auditing(AuditUtil auditUtil) {
        // 查询要审核的采购单
        YxCommonPurchase purchase = baseMapper.selectById(auditUtil.getRecordId());
        // 修改审核状态
        purchase.setStatus(auditUtil.getState());
        return baseMapper.updateById(purchase);
    }

    @Override
    public BigDecimal showCostByToday() {
        // 创建日期计算器
        GregorianCalendar calendar = new GregorianCalendar();
        // 设置传入时间
        calendar.setTime(new Date());
        // 设置时分秒为零
        removeHourMinuteSecond(calendar);
        // 开始时间当天零点
        calendar.add(Calendar.DATE,-1);
        // 生成开始时间
        Date startTime = calendar.getTime();
        return baseMapper.selectByTime(startTime);
    }

    @Override
    public BigDecimal showCostByWeek() {
        // 创建日期计算器
        GregorianCalendar calendar = new GregorianCalendar();
        // 设置传入时间
        calendar.setTime(new Date());
        // 设置时分秒为零
        removeHourMinuteSecond(calendar);
        // 生成开始时间
        Date endTime = calendar.getTime();
        // 开始时间当周第一天
        calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        Date startTime = calendar.getTime();
        return baseMapper.selectByTime(startTime);
    }

    @Override
    public BigDecimal showCostByMonth(){
        // 创建日期计算器
        GregorianCalendar calendar = new GregorianCalendar();
        // 设置传入时间
        calendar.setTime(new Date());
        // 设置时分秒为零
        removeHourMinuteSecond(calendar);
        // 生成开始时间
        Date endTime = calendar.getTime();
        // 开始时间当月第一天
        calendar.set(Calendar.DAY_OF_MONTH,1);
        Date startTime = calendar.getTime();
        return baseMapper.selectByTime(startTime);
    }

    @Override
    public BigDecimal showCostByYear() {
        // 创建日期计算器
        GregorianCalendar calendar = new GregorianCalendar();
        // 设置传入时间
        calendar.setTime(new Date());
        // 设置时分秒为零
        removeHourMinuteSecond(calendar);
        // 开始时间当年第一天
        calendar.set(Calendar.DAY_OF_YEAR,1);
        Date startTime = calendar.getTime();
        return baseMapper.selectByTime(startTime);
    }


    private void removeHourMinuteSecond(GregorianCalendar calendar) {
        // 设置 时
        calendar.set(Calendar.HOUR_OF_DAY,0);
        // 设置 分
        calendar.set(Calendar.MINUTE,0);
        // 设置 秒
        calendar.set(Calendar.SECOND,0);
        // 设置 毫秒
        calendar.set(Calendar.MILLISECOND,0);
    }
}